// import Vue from "vue";
// 引入请求的路径
// import userService from "@/api/user/index";
// import { setToken } from "@/utils/auth";
// import zhrequest from "@/api/axios";
// import { webServiceUrl } from "@/api/baseURL";
// import getfarmreq from "@/api/basic-info-manage/agricultural-type-manage/index";
import Layout from '@/layout';
import userService from '@/api/user/index';

import { asyncRoutes, constantRoutes } from '@/router';
import { getToken } from '@/utils/auth';

const _import = require('../../router/_import_' + process.env.NODE_ENV); // 获取组件的方法
function filterAsyncRouter(asyncRouterMap) {
  // 遍历后台传来的路由字符串，转换为组件对象
  const accessedRouters = asyncRouterMap.filter(route => {
    if (route.component) {
      if (route.component === 'Layout') {
        // Layout组件特殊处理
        route.component = Layout;
      } else {
        // console.log(_import(route.component))
        route.component = _import(route.component);
      }
    }
    // if (route.routeId) {
    //   delete route.routeId
    // }
    // if (route.parentId) {
    //   delete route.parentId
    // }
    // if (route.meta.metaId) {
    //   delete route.meta.metaId
    // }
    // if (route.meta.routeId) {
    //   delete route.meta.routeId
    // }
    // if (route.children && route.children.length > 0) {
    //   route.children = filterAsyncRouter(route.children);
    // }
    if (route.children) {
      route.children = filterAsyncRouter(route.children);
    }
    return true;
  });
  return accessedRouters;
}
// function componentuser (url) {
//   import(url)
// }
const state = {
  routes: [],
  addRoutes: [],
  pageElementList: [],
  _axiosPromiseArr: []
};
const mutations = {
  SET_ROUTES: (state, routes) => {
    state.addRoutes = routes;
    state.routes = constantRoutes.concat(routes);
  },
  SET_PAGE_ELEMENT: (state, payload) => {
    state.pageElementList = payload;
  },
  add_AxiosPromiseArr(state, item) {
    // console.log("添加_axiosPromiseArr");
    state._axiosPromiseArr.push(item);
  },
  clear_AxiosPromiseArr(state) {
    // console.log("_axiosPromiseArr情空");
    state._axiosPromiseArr = [];
  }
};

const getters = {
  judgePermission: state => {
    return (pageNo, code) => {
      // console.log(pageNo, code)
      // eslint-disable-next-line eqeqeq
      let index = state.pageElementList.findIndex(el => el.pageNo == pageNo && el.code == code);
      if (index > -1) {
        return true;
      } else {
        return false;
      }
    };
  }
};
// 是否启用动态路由 true false
const nowmenutype = true;
const actions = {
  generateRoutes({ commit }) {
    // eslint-disable-next-line no-async-promise-executor
    return new Promise(async resolve => {
      commit('SET_ROUTES', []);
      let accessedRoutes;
      let virtualSystemNo = getToken('virtualSystemNo');
      if (nowmenutype) {
        let res = await userService.queryMenuAlls(virtualSystemNo);
        if (res.code !== 1) {
          res = await userService.queryMenuAll(virtualSystemNo);
        }
        let testjk = [];
        let zhrouter = res.data || [];
        testjk.push(...zhrouter);
        let router404 = {
          path: '*',
          redirect: '/404',
          hidden: true
        };
        testjk.push(router404);
        accessedRoutes = filterAsyncRouter(testjk);
        commit('SET_ROUTES', accessedRoutes);
        let pageElementRes = await userService.queryPageElement(virtualSystemNo);
        commit('SET_PAGE_ELEMENT', pageElementRes.data);
        resolve(accessedRoutes);
      } else {
        let testjk = [];
        // 新增一个最后的404路由
        let router404 = {
          path: '*',
          redirect: '/404',
          hidden: true
        };
        testjk.push(...asyncRoutes);
        testjk.push(router404);
        accessedRoutes = filterAsyncRouter(testjk);
        commit(
          'SET_ROUTES',
          accessedRoutes.sort((a, b) => a.index - b.index)
        );
        resolve(accessedRoutes);
      }
      // ----------------//

      // ----------原代码----
      // 接口获取asyncRoutes
      // Vue.prototype.$service.get("/router").then(res => {
      //   let data = res.data;
      //   // 新增一个公用的404路由
      //   let router404 = {
      //     path: "*",
      //     redirect: "/404",
      //     hidden: true
      //   };
      //   data.push(router404);
      //   let apiRouter = "";
      //   apiRouter = filterAsyncRouter(data);
      //   console.log(apiRouter);
      //   commit("SET_ROUTES", accessedRoutes);
      //   resolve(accessedRoutes);
      // });

      // accessedRoutes = asyncRoutes;
      // commit("SET_ROUTES", accessedRoutes);
      // resolve(accessedRoutes);
    });
  }
};

export default {
  namespaced: true,
  state,
  mutations,
  getters,
  nowmenutype,
  actions
};
